<template>
  <view>
    <view class='productList'>

        <uv-sticky :offsetTop="0" :customNavHeight="0" bgColor="#fff">
          <view class="heightWrapper">
          <view class="search">
            <view class="order-total-text">
              数量：{{ dataPage.total }}
            </view>

            <view @click="filterCondition" class="total-search">
              <view class="school-class">筛选</view>
              <uv-action-sheet
                  ref="actionSheet"
                  :actions="schools"
                  title="学校筛选"
                  @select="selectSchool">
              </uv-action-sheet>
            </view>
          </view>
          </view>
        </uv-sticky>

      <PageBody :properties="pageBody" @scrolltolower="scrolltolower">
        <template v-slot:body>
          <uv-gap height="10" v-if="datas.length > 0" bgColor=""></uv-gap>
          <view v-if="datas.length > 0" v-for="(item,index) in datas">
            <view class="student-wrapper">
              <view class="student-item">
                <view class="avatar-line">
                  <view class="avatar">
                    <uv-avatar v-if="item.avatar" :size="50" :src="item.avatar"></uv-avatar>
                    <uv-avatar v-else :text="item.realName ? item.realName : item.nickName" fontSize="12" randomBgColor></uv-avatar>
                  </view>
                  <view>
                    <view>姓名：{{ item.realName ? item.realName : item.nickName }}（性别：{{
                        (item.sex === null || item.sex === '0') ? '未知' : item.sex === '1' ? '男' : '女'
                      }}）
                    </view>
                    <view>联系电话：{{ item.phone }}</view>
                  </view>
                  <view>
                    <!--   学生认证状态:0=待审核，1=审核通过，2=审核不通过-->
                    <Dict :properties="{dictType:'member_state',dictValue:item.state,size:'mini'}"/>
                  </view>
                </view>
                <view>
                  <view class="item-line">
                    <view>用户ID：{{ item.id }}</view>
                  </view>

                  <view class="item-line">
                    <view>昵称：{{ item.nickName }}</view>
                  </view>

                  <view class="item-line">
                    <view>真名：{{ item.realName==null?'':item.realName }}</view>
                  </view>

                  <view class="item-line">
                    <view>注册时间：{{ item.createTime }}</view>
                  </view>

                  <view class="item-line">
                    代理：
                    <view style="margin-right: 10px" @click="openAgentManager(item)">
                      <uv-text type="primary" size="14" text="设置"></uv-text>
                    </view>
                      <view>
                        {{ item.isAgent ? '' : '否' }}
                        <text v-if="item.isAgent">
                          [{{ item.agentInfo !== null ? item.agentInfo.schoolName : '' }}]
                        </text>
                      </view>

                  </view>

                  <view class="item-line">
                    <view>
                      学校：
                      {{ item.schoolInfo !== null ? item.schoolInfo.schoolName : '' }}
                    </view>
                  </view>

                  <view class="item-line">
                    <view>权限：</view>
                    <view @click="openPermissionManager(item)">
                      <uv-text type="primary" size="14" text="管理"></uv-text>
                    </view>
                  </view>

                  <view v-if="item.permissions!==null" style="display: flex;flex-wrap: wrap;align-items: center">
                    <view style="margin: 10rpx" v-for="it in item.permissions">
                      <uv-tags :text="it.name" plain="" type="warning"></uv-tags>
                    </view>
                  </view>


                </view>
                <view class="submit-btn" v-if="item.state===0">
                </view>
              </view>
            </view>
          </view>


          <Empty :show="datas.length<=0"/>

          <uv-load-more v-if="isReachBottom" @loadmore="loadmore" :status="loading.status" :loading-text="loading.loadingText"
                        :loadmore-text="loading.loadmoreText"
                        nomore-text=" "/>
          <uv-gap height="10" v-if="isReachBottom"/>

        </template>
      </PageBody>
    </view>

    <!--    搜索条件-->
    <uv-popup @close="clearForm" mode="top" closeable
              closeIconPos="bottom-right"
              :closeOnClickOverlay="false"
              custom-style="border-radius:10px" ref="popup">
      <view style="padding: 30rpx">
        <view style="margin-top: 10rpx">

          <view class="search-item">
            <view class="field">ID：</view>
            <view class="field-value">
              <uv-input placeholder="用户ID" v-model="searchForm.id" clearable border="bottom"></uv-input>
            </view>
          </view>

          <view class="search-item">
            <view class="field">学校：</view>
            <view @click="openSchool" class="field-value">
              <uv-input readonly placeholder="学校" v-model="searchForm.schoolName" clearable border="bottom"></uv-input>
              <uv-action-sheet
                  ref="actionSheet"
                  :actions="schools"
                  title="学校筛选"
                  @select="selectSchool">
              </uv-action-sheet>
            </view>
          </view>

          <view class="search-item">
            <view class="field">手机号：</view>
            <view class="field-value">
              <uv-input placeholder="手机号" v-model="searchForm.phone" clearable border="bottom"></uv-input>
            </view>
          </view>

          <view class="search-btn">

            <view class="btn">
              <uv-button :customTextStyle="{color:themConfig.theme.color}" :iconColor="themConfig.theme.color"
                         text="清空" @click="clearForm" type="default"></uv-button>
            </view>
            <view class="btn">
              <uv-button :customTextStyle="{color:themConfig.theme.color}" :iconColor="themConfig.theme.color"
                         text="搜索" @click="submitSearchForm" type="default"></uv-button>
            </view>
          </view>
        </view>
      </view>
    </uv-popup>

    <!-- 权限管理-->
    <uv-popup @close="clearForm" mode="bottom" closeable
              closeIconPos="top-right"
              :closeOnClickOverlay="false"
              custom-style="border-radius:10px" ref="permission_popup">
      <view style="padding: 60rpx 30rpx 30rpx 30rpx">
        <view style="margin-top: 10rpx">

          <view class="item-line">
            <view style="display: flex;flex-wrap: wrap;align-items: center">
              所有权限：
              <view style="margin: 10rpx" v-for="it in permissionDatas">
                <uv-tags @click="addPermisions(it)" :text="it.name" plain="" type="warning"></uv-tags>
              </view>
            </view>
          </view>

          <view class="item-line"
                v-if=" currentItemPermis !=null && (currentItemPermis.code==='student_manager' || currentItemPermis.code==='student_audit')">
            <view style="display: flex;flex-direction: column;flex-wrap: wrap;justify-content: left">
              <view style="display: flex;align-items: center">选择学校权限：
                <uv-tags :text="currentItemPermis.name" plain=""></uv-tags>
                <uv-icon style="margin-left: 10px" size="25" name="plus-circle" @click="switchSchoolTab"></uv-icon>
              </view>
              <view style="display: flex !important;flex-wrap: wrap;justify-content: left">
                <view style="margin: 10rpx" v-for="(it,idx) in schoolInfos">
                  <uv-tags @close="closeSchoolsPermiss(idx)" closable :text="it.schoolName" plain="" type="success"></uv-tags>
                </view>
              </view>
            </view>
          </view>


          <view class="item-line" v-if="viewItem.permissions!==null">
            <view style="display: flex;flex-wrap: wrap;align-items: center;margin-top: 20px">
              用户权限：
              <view style="margin: 10rpx" v-for="(it,idx) in viewItem.permissions">
                <uv-tags @close="closeMemberPermiss(idx)" closable :text="it.name" plain="" type="warning"></uv-tags>
              </view>
            </view>
          </view>

          <view class="search-btn">
            <view class="btn">
              <uv-button :color="themConfig.theme.color" :iconColor="themConfig.theme.color"
                         text="保存" @click="submitPermission"></uv-button>
            </view>
          </view>
        </view>
      </view>
    </uv-popup>


    <!--    设置代理-->
    <uv-popup @close="closeAgentForm" mode="bottom" closeable
              closeIconPos="top-right"
              :closeOnClickOverlay="false"
              custom-style="border-radius:10px" ref="agent_popup">
      <view style="padding: 60rpx 30rpx 10rpx 30rpx">
        <view style="margin-top: 10rpx">

          <uv-form labelWidth="120" labelPosition="left" :model="agentForm" ref="form">

            <uv-form-item label="是否代理" prop="isAgent" borderBottom @click="">
              <uv-radio-group v-model="agentForm.isAgent" :activeColor="themConfig.theme.color">
                <uv-radio
                    :customStyle="{margin: '8px'}"
                    v-for="(item, index) in [{name:'代理',value: true},{name:'非代理',value: false}]"
                    :key="index"
                    :label="item.name"
                    :name="item.value">
                </uv-radio>
              </uv-radio-group>
            </uv-form-item>

            <uv-form-item label="代理学校" @click="switchSchoolTab" prop="schoolName" borderBottom>
              <uv-input @click="switchSchoolTab" readonly placeholder="代理学校" v-model="agentForm.schoolName" border="none">
              </uv-input>
              <template v-slot:right>
                <uv-icon @click="switchSchoolTab" name="arrow-right"></uv-icon>
              </template>
            </uv-form-item>
          </uv-form>

          <view class="search-btn">
            <view class="btn">

              <uv-button :color="themConfig.theme.color" :iconColor="themConfig.theme.color"
                         text="保存" @click="submitAgent"></uv-button>
            </view>
          </view>
        </view>
      </view>
    </uv-popup>
  </view>
</template>
<script>
import {mixins} from './index.js'

export default {
  mixins: [mixins]
}
</script>

<style scoped lang="scss">
@import './index.scss';
</style>
